package com.esm.sm800.database.manager;

import com.esm.sm800.activity.AppContext;
import com.esm.sm800.database.dbmodel.Draw_report;
import com.esm.sm800.database.greendao.Draw_reportDao;

import java.util.ArrayList;

public class DrawreportCenter {

    private static DrawreportCenter instance = new DrawreportCenter();

    public static DrawreportCenter getInstance() {
        return instance;
    }

    public Draw_report getDraw_report(long uid, long nid) {
        if (uid < 1 || nid < 1) {
            return null;
        }

        Draw_report Draw_report = null;
        try {
            Draw_reportDao Draw_reportDao = AppContext.getInstances().getDaoSession().getDraw_reportDao();
            ArrayList<Draw_report> list = new ArrayList<Draw_report>();
            list = (ArrayList) Draw_reportDao.queryBuilder()
                    .where(com.esm.sm800.database.greendao.Draw_reportDao.Properties.Nid.eq(nid))
                    .where(com.esm.sm800.database.greendao.Draw_reportDao.Properties.Uid.eq(uid))
                    .build().list();
            if (list.size() > 0) {
                Draw_report = list.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Draw_report;
    }

    public Draw_report getDraw_reporthz(long uid, long nid) {
        if (uid < 1 || nid < 1) {
            return null;
        }

        Draw_report Draw_report = null;
        try {
            Draw_reportDao Draw_reportDao = AppContext.getInstances().getDaoSession().getDraw_reportDao();
            ArrayList<Draw_report> list = new ArrayList<Draw_report>();
            list = (ArrayList) Draw_reportDao.queryBuilder()
                    .where(com.esm.sm800.database.greendao.Draw_reportDao.Properties.Nid.eq(nid))
                    .where(com.esm.sm800.database.greendao.Draw_reportDao.Properties.Uid.eq(uid))
                    .where(com.esm.sm800.database.greendao.Draw_reportDao.Properties.Type.eq(1))
                    .build().list();
            if (list.size() > 0) {
                Draw_report = list.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Draw_report;
    }

    public Long saveDraw_report(Draw_report Draw_report) {
        if (Draw_report == null) {
            return 0L;
        }

        try {
            Draw_reportDao Draw_reportDao = AppContext.getInstances().getDaoSession().getDraw_reportDao();
            long id = Draw_reportDao.insertOrReplace(Draw_report);
            return id;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0L;
    }

    public Long insertDraw_report(Draw_report Draw_report) {
        if (Draw_report == null) {
            return 0L;
        }

        try {
            Draw_reportDao Draw_reportDao = AppContext.getInstances().getDaoSession().getDraw_reportDao();
            long id = Draw_reportDao.insertOrReplace(Draw_report);
            return id;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0L;
    }

    //还针记录
    public ArrayList<Draw_report> getDraw_reportlist(String startdate, String enddate, int type) {
        ArrayList<Draw_report> list = new ArrayList<Draw_report>();
        try {
            Draw_reportDao Draw_reportDao = AppContext.getInstances().getDaoSession().getDraw_reportDao();
               if (startdate.length() < 1 || enddate.length() < 1 || startdate.compareTo(enddate) > 0) {
                list = (ArrayList) Draw_reportDao.queryBuilder()
                        .where(com.esm.sm800.database.greendao.Draw_reportDao.Properties.Type.eq(type))
                        .orderDesc(com.esm.sm800.database.greendao.Draw_reportDao.Properties.Return_time)
                        .build().list();
            } else {
                startdate += " 00:00:00";
                enddate += " 23:59:59";
                list = (ArrayList) Draw_reportDao.queryBuilder()
                        .where(com.esm.sm800.database.greendao.Draw_reportDao.Properties.Type.eq(type))
                        .where(com.esm.sm800.database.greendao.Draw_reportDao.Properties.Return_time.ge(startdate))
                        .where(com.esm.sm800.database.greendao.Draw_reportDao.Properties.Return_time.le(enddate))
                        .orderDesc(com.esm.sm800.database.greendao.Draw_reportDao.Properties.Return_time)
                        .build().list();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }

    public ArrayList<Draw_report> drlist = null;

    //未还针的记录
    public ArrayList<Draw_report> getDraw_reportlistbyuid(long uid) {
        ArrayList<Draw_report> list = new ArrayList<Draw_report>();
        try {
            Draw_reportDao Draw_reportDao = AppContext.getInstances().getDaoSession().getDraw_reportDao();
            list = (ArrayList) Draw_reportDao.queryBuilder()
                    .where(com.esm.sm800.database.greendao.Draw_reportDao.Properties.Uid.eq(uid))
                    .where(com.esm.sm800.database.greendao.Draw_reportDao.Properties.Type.eq(0))
                    .orderDesc(com.esm.sm800.database.greendao.Draw_reportDao.Properties.Update_time)
                    .build().list();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }

    public ArrayList<Draw_report> getDraw_reportlistbyuidandname(long uid, String needlename) {
        ArrayList<Draw_report> list = new ArrayList<Draw_report>();
        try {
            Draw_reportDao Draw_reportDao = AppContext.getInstances().getDaoSession().getDraw_reportDao();
            list = (ArrayList) Draw_reportDao.queryBuilder()
                    .where(com.esm.sm800.database.greendao.Draw_reportDao.Properties.Uid.eq(uid))
                    .where(com.esm.sm800.database.greendao.Draw_reportDao.Properties.Needle_name.like(needlename))
                    .where(com.esm.sm800.database.greendao.Draw_reportDao.Properties.Type.eq(0))
                    .orderDesc(com.esm.sm800.database.greendao.Draw_reportDao.Properties.Update_time)
                    .build().list();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }
}
